#!/bin/bash -e

# include required packages
source ./paths.sh
source ./get-package.sh

function basicJail	{

# first argument needs to be the name of the jail
# second argument needs to be the name of the user

export JAIL_NAME=$1
export JAIL_USER=$2
export JAIL_GRP=tnick
export JAIL_PATH="$PATH_OUT/$JAIL_NAME"
J_L1_SCRIPT="$PATH_SCRIPTS/tools-l1.sh"
J_L2_SCRIPT="$PATH_SCRIPTS/tools-l2.sh"
J_L3_SCRIPT="$PATH_SCRIPTS/tools-l3.sh"


# present the header
echo	"


=====================================================================

                 Utility to create a basic jail
 
=====================================================================


"

#~ # =============
#~ asdasdsadasda=false
#~ if $asdasdsadasda; then
#~ # =============


# remove traces of old runs
sudo chown -v tnick $PATH_TCACHE
sudo chown -v tnick $PATH_PKGCACHE
if [ -h "/tools" ]; then
	sudo rm /tools
fi
if [ -h "/build-dir" ]; then
	sudo rm /build-dir
fi

	#~ sudo umount $LFS/dev/pts > /dev/null
	#~ sudo umount $LFS/dev/shm > /dev/null
	#~ sudo umount $LFS/dev > /dev/null
	#~ sudo umount $LFS/proc > /dev/null
	#~ sudo umount $LFS/sys  > /dev/null
	#~ sudo rm -rfv $LFS/dev > /dev/null
	#~ sudo rm -rfv $LFS/proc > /dev/null
	#~ sudo rm -rfv $LFS/sys  > /dev/null
	#~ #sudo rm -rfv $LFS/tools > /dev/null



# make sure jail group exists
grp_check=$(grep -i "$JAIL_GRP" /etc/group)
if [ -z "$grp_check" ]; then
	echo	"Creating usergroup   <jail>"
	sudo groupadd $JAIL_GRP
fi


# make sure user exists
if id -u "$JAIL_USER" >/dev/null 2>&1; then
	echo	"User    <$JAIL_USER>    already exists."
else
	echo	"
	Using <$JAIL_USER> as user. 
	- user login shell: /bin/bash
	- group: jail
	- creating empty home directory: /home/$JAIL_USER
	Password for new user account is now requested.

	"
	sudo useradd -s /bin/bash -g $JAIL_GRP -m -k /dev/null $JAIL_USER
	sudo passwd $JAIL_USER
fi


# create basic folder tree
if [ -d $JAIL_PATH ]; then
	sudo rm -rfv $JAIL_PATH > /dev/null
fi
mkdir $JAIL_PATH
cd $JAIL_PATH
mkdir $JAIL_PATH/tools
mkdir $JAIL_PATH/build-dir

echo	"create a /tools symlink on the host system."
if [ ! -h "/tools" ]; then
	sudo ln -sv "$JAIL_PATH/tools" /
fi
if [ ! -h "/build-dir" ]; then
	sudo ln -sv "$JAIL_PATH/build-dir" /
fi

echo	"User $JAIL_USER should own $JAIL_PATH/tools"
sudo chown -v $JAIL_USER $JAIL_PATH/build-dir
sudo chown -v $JAIL_USER $JAIL_PATH/tools
sudo chown -v $JAIL_USER $PATH_TCACHE
sudo chown -v $JAIL_USER $PATH_PKGCACHE
sudo chown -v $JAIL_USER /build-dir
sudo chown -v $JAIL_USER /tools


# either create level 3 tools or extract them
if [ -f "$PATH_L3_TCACHE" ]; then
	echo	"Extracting cached tools from"
	echo	"$PATH_L3_TCACHE"
	sudo rm -rfv $JAIL_PATH/tools/* > /dev/null
	
	cd $JAIL_PATH
	sudo tar -zxf $PATH_L3_TCACHE
else


	# either create level 2 tools or extract them
	if [ -f "$PATH_L2_TCACHE" ]; then
		echo	"Extracting cached tools from"
		echo	"$PATH_L2_TCACHE"
		sudo rm -rfv $JAIL_PATH/tools/* > /dev/null
		
		cd $JAIL_PATH
		sudo tar -zxf $PATH_L2_TCACHE
	else

		# either create level 1 tools or extract them
		if [ -f "$PATH_L1_TCACHE" ]; then
			echo	"Extracting cached tools from"
			echo	"$PATH_L1_TCACHE"
			sudo rm -rfv $JAIL_PATH/tools/* > /dev/null
			
			cd $JAIL_PATH
			sudo tar -zxf $PATH_L1_TCACHE
		else
			cp $J_L1_SCRIPT "$JAIL_PATH/tools-l1.sh"
			sudo chown -v $JAIL_USER "$JAIL_PATH/tools-l1.sh"
			
			echo	"Login as $JAIL_USER"
			sudo -u $JAIL_USER -g $JAIL_GRP -E -H bash -c ./tools-l1.sh
			
			echo	"level 1 tools are now being archived for future use..."
			echo	"from $JAIL_PATH/tools"
			echo	"to $PATH_L1_TCACHE"
			if [ -f "$PATH_L1_TCACHE" ]; then
				sudo rm $PATH_L1_TCACHE
			fi
			cd $JAIL_PATH
			sudo tar -pczf "$PATH_L1_TCACHE" "./tools"

		fi


		# level 2 tools
		cp $J_L2_SCRIPT "$JAIL_PATH/tools-l2.sh"
		sudo chown -v $JAIL_USER "$JAIL_PATH/tools-l2.sh"
		
		echo	"Login as $JAIL_USER"
		sudo -u $JAIL_USER -g $JAIL_GRP -E -H bash -c ./tools-l2.sh
		
		echo	"level 2 tools are now being archived for future use..."
		echo	"from $JAIL_PATH/tools"
		echo	"to $PATH_L2_TCACHE"
		if [ -f "$PATH_L2_TCACHE" ]; then
			sudo rm $PATH_L2_TCACHE
		fi
		cd $JAIL_PATH
		sudo tar -pczf "$PATH_L2_TCACHE" "./tools"

	fi

	# level 3 tools
	cp $J_L3_SCRIPT "$JAIL_PATH/tools-l3.sh"
	sudo chown -v $JAIL_USER "$JAIL_PATH/tools-l3.sh"
	
	echo	"Login as $JAIL_USER"
	sudo -u $JAIL_USER -g $JAIL_GRP -E -H bash -c ./tools-l3.sh
	
	echo	"level 3 tools are now being archived for future use..."
	echo	"from $JAIL_PATH/tools"
	echo	"to $PATH_L3_TCACHE"
	if [ -f "$PATH_L3_TCACHE" ]; then
		sudo rm $PATH_L3_TCACHE
	fi
	cd $JAIL_PATH
	sudo tar -pczf "$PATH_L3_TCACHE" "./tools"

fi




#~ 
#~ # =============
#~ fi
#~ # =============
#~ 
#~ 
	#~ 
#~ # =============


sudo chown -v tnick $PATH_TCACHE
sudo chown -v tnick $PATH_PKGCACHE


# present the footer
echo	"


=====================================================================

              Utility to create a basic jail done
 
=====================================================================



"
}

basicJail jail1 Jail1


